﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace KMeansDemo
{
    /// <summary>
    /// vs = Common Values，即常用值
    /// </summary>
    public class common
    {
        public static double NodeSize = 4;

        public static void SetTargetToStoryboard(Animatable animatable, string targetName, DependencyProperty dp)
        {
            Storyboard.SetTargetName(animatable, targetName);
            Storyboard.SetTargetProperty(animatable, new PropertyPath(dp));
        }

        public static double SetTargetToStoryboard(Storyboard storyboard, AnimationTimeline animatable, string targetName, DependencyProperty dp)
        {
            Storyboard.SetTargetName(animatable, targetName);
            Storyboard.SetTargetProperty(animatable, new PropertyPath(dp));
            storyboard.Children.Add(animatable);
            return GetTotalTime(storyboard);//.Duration.TimeSpan.TotalSeconds;
        }

        public static double SetTargetToStoryboard(Storyboard storyboard, AnimationTimeline animatable, DependencyObject target, DependencyProperty dp)
        {
            Storyboard.SetTarget(animatable, target);
            Storyboard.SetTargetProperty(animatable, new PropertyPath(dp));
            storyboard.Children.Add(animatable);
            return GetTotalTime(storyboard);//.Duration.TimeSpan.TotalSeconds;
        }

        public static double GetTotalTime(Storyboard storyboard)
        {
            double totalTime = 0;
            foreach (var item in storyboard.Children)
            {
                if (item.BeginTime != null)
                {
                    double t = (item.BeginTime + item.Duration.TimeSpan).Value.TotalMilliseconds;
                    if (t > totalTime)
                        totalTime = t;
                }
            }

            return totalTime/1000;
        }

        public static Ellipse SetStyle(Ellipse e, EllipseStyle style)
        {
            switch (style)
            {
                case EllipseStyle.Point:
                    int r1 = 2;
                    e.Width = r1* 2;
                    e.Height = r1 * 2;
                    e.Fill = Brushes.Blue;
                    e.Stroke = Brushes.Black;
                    e.StrokeThickness = 0.5;
                    break;

                case EllipseStyle.Centroid:
                    int r2 = 5;
                    e.Width = r2 * 2;
                    e.Height = r2 * 2;
                    e.Fill = Brushes.Blue;
                    e.Stroke = Brushes.Black;
                    e.StrokeThickness = 0.5;
                    break;
                default:
                case EllipseStyle.Contour:
                    int r3 = 6;
                    e.Width = r3 * 2;
                    e.Height = r3 * 2;
                    e.Stroke = Brushes.DarkGray;
                    e.StrokeThickness = 12;
                    break;
            }
            return e;
        }

        public static DoubleAnimation CreateDoubleAnimation(double from, double to, double duration_seconds = 1, double beginTime_seconds = 0)
        {
            DoubleAnimation dax = new DoubleAnimation();
            dax.BeginTime = TimeSpan.FromSeconds(beginTime_seconds);
            dax.Duration = TimeSpan.FromSeconds(duration_seconds);
            dax.From = from;
            dax.To = to;
            return dax;
        }


        public static DoubleAnimation CreateDoubleAnimation(Storyboard storyboard, double from, double to, double duration_seconds = 1, double beginTime_seconds = 0)
        {
            DoubleAnimation dax = new DoubleAnimation();
            dax.BeginTime = TimeSpan.FromSeconds(beginTime_seconds);
            dax.Duration = TimeSpan.FromSeconds(duration_seconds);
            dax.From = from;
            dax.To = to;
            return dax;
        }


        /// <summary>
        /// Equiverlant to Button.PerformClick in traditional framework.
        /// </summary>
        /// <param name="button">A Button to invoke</param>
        public static void PerformClick(ButtonBase button)
        {
            var method = button.GetType().GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
            if (method != null)
            {
                method.Invoke(button, null);
                button.Focus();
            }
        }

    }
}
